package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.job.ums.entity.UmsChatPreset;
import org.jeecg.modules.job.ums.entity.UmsRobotLog;
import org.jeecg.modules.job.ums.service.IUmsChatPresetService;
import org.jeecg.modules.job.ums.service.IUmsRobotLogService;
import org.jeecg.modules.job.wms.entity.WmsResume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *智能客服
 */
@RestController
@RequestMapping("/api/chat")
@Api(tags="移动端智能客服管理模块")
@Slf4j
public class ChatController{

    @Autowired
    private IUmsChatPresetService chatPresetService;
    @Autowired
    private IUmsRobotLogService robotLogService;

    /**
     * 查询常见问题列表
     */
    @ResponseBody
    @RequestMapping(value = "/getChatAskList",method = RequestMethod.GET)
    @ApiOperation(value = "查询常见问题列表", notes = "查询常见问题列表")
    public Result<Object> getChatAskList(UmsChatPreset param,
                                         @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                         @RequestParam(name="pageSize", defaultValue="20") Integer pageSize) {
        IPage<UmsChatPreset> pageList = chatPresetService.page(new Page<>(pageNo, pageSize),new QueryWrapper<>(param));
        return Result.ok(pageList);
    }

    /**
     * 查询回复信息
     */
    @ResponseBody
    @RequestMapping(value = "/getChatReply",method = RequestMethod.GET)
    @ApiOperation(value = "查询回复信息", notes = "查询回复信息")
    public Result<Object> getChatReply(UmsChatPreset param) {
        List<UmsChatPreset> list=chatPresetService.list(new QueryWrapper<>(param));
        if (list.size()>0){
            return Result.ok(list.get(0));
        }
        return Result.ok();
    }


    /**
     *  添加智能聊天记录
     */
    @ResponseBody
    @RequestMapping(value = "/addRobotLog",method = RequestMethod.POST)
    @ApiOperation(value = "添加智能聊天记录", notes = "添加智能聊天记录")
    public Result<Object> addRobotLog(@RequestBody UmsRobotLog param) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            robotLogService.save(param);
            return Result.ok();
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询智能聊天记录
     */
    @ResponseBody
    @RequestMapping(value = "/getRobotLogList",method = RequestMethod.GET)
    @ApiOperation(value = "查询智能聊天记录", notes = "查询智能聊天记录")
    public Result<Object> getRobotLogList(UmsRobotLog param,
                                          @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                          @RequestParam(name="pageSize", defaultValue="20") Integer pageSize) {
        try {
            LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            param.setMemberId(user.getId());
            IPage<UmsRobotLog> pageList = robotLogService.page(new Page<>(pageNo, pageSize),new QueryWrapper<>(param));
            return Result.ok(pageList);
        }catch (Exception e){
            System.err.println(e.getMessage());
            return Result.ok();
        }
    }

}



